<template>

  <el-dialog  title="流程设置" :visible.sync="dialogStatus" width="90%"   center :modal-append-to-body="false">
      <el-steps  align-center >
        <el-step title="编码申请发起" description="" status="process"></el-step>
        <el-step status="finish" icon="el-icon-edit"
                 :description="descript?'修改':'尚未设置'"
                 :title="'设置审核人'+(index+1)"
                 v-for="(item,index) in userNodeInfo" :key="index"
                 @click.native="FlowSetUserNode(item)"
        ></el-step>
        <el-step title="结束"  description=""></el-step>
      </el-steps>

      <el-row v-show="divVisible">
        <el-col :span="24" class="flowdiv">

          <el-form :rules="rules" ref="dataForm" :model="temp" label-position="right" label-width="20%" style='width: 90%; margin-left:20vw;'>

            <el-form-item label="指定范围" prop="rangeList">
                <span style="color:mediumvioletred;font-weight:bolder">{{temp.userAllotType|$_filterType}}</span>
            </el-form-item>
            <el-form-item :label="'指定'+_UserAllotType[temp.userAllotType]" prop="ids">
              <el-select v-model="temp.ids" multiple placeholder="请选择"  style="width:40%;min-width:200px;">
                <el-option
                  v-for="item in selectUserAllot"
                  :key="item._id"
                  :label="item.allotName"
                  :value="item._id">
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="" >
                <el-button @click="divVisible = false">取 消</el-button>
                <el-button  @click="FlowSubmitUserNode" type="primary" id="cfmbtm1">确定</el-button>
            </el-form-item>
          </el-form>



        </el-col>
    </el-row>
  </el-dialog>
</template>
<script>

  import {fetchGet,positionAll,departAll,userAll,createOne} from '@/api/common'
  import {disableBtn,ERRS, SUCS} from '@/utils'
  import mixinAr from './mixinAr'
  export default {
    name: 'flowEdit',
    inject: ['_company'],
    mixins: [mixinAr],
    props: {
      // levStatus: Number,
      row:Object,
    },
    data:function () {
      return {
        divVisible: false,
        dialogStatus:false,
        listLoading:false,
        //审核信息
        descript:false,
        userNodeInfo:{},
        companyId:'',
        temp: {},
        selectUserAllot :[],

        rules: {}
      }
    },
    watch:{

    },
    methods: {
      //编辑row 请求节点信息
      getNodeInfo(row){
        this.companyId =this._company._id,
        this.groupId = row._id,
        console.log(row)
        //审核节点
        fetchGet(`flowable/dy/${this.companyId}/${this.groupId}/nodeOpUser`).then(res => {
          console.log(res);
          if (res.code == 0) {
            let dat = res.data
            console.log(dat)
              this.userNodeInfo=dat;
              if(dat[0].nodeOpUser)
                this.descript=true;

          } else {

          }
          this.listLoading = false
        })

        this.dialogStatus = true
      },


      /* -------         curd        ---------- */
      FlowSetUserNode(it){
        console.log(111,it);
        let nu=it.nodeUser
        let allotText=it.nodeOpUser?it.nodeOpUser.allotText:false

        this.getSelectUserAllot(nu.userAllotType)
        this.temp={
          id: undefined,
          companyId :this.companyId,
          groupId :nu.groupId,
          nodeId  :nu.nodeId,
          userAllotType  :nu.userAllotType,
          // ids : nou.allotText?  nou.allotText :[],
          ids : allotText? eval('(' + allotText + ')') :[],
        },
        console.log()
        this.divVisible=true;
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },

      //获取select数据
      getSelectUserAllot(Type) {
        // Type='DEPARTMENT'
        if(Type=='USER'){
          userAll(this.companyId).then(res => {
            if (res.code == 0) {
              this.insertWord(res.data,Type);
            } else {
              ERRS('部门调用Err: '+res.message)
            }
          })
        }else if(Type=='POSITION'){

          positionAll(this.companyId).then(res => {
            if (res.code == 0) {
              this.insertWord(res.data,Type);
            } else {
              ERRS('职位调用Err: '+res.message)
            }
          })
        }else if(Type=='DEPARTMENT'){
          departAll(this.companyId).then(res => {
            if (res.code == 0) {
              this.insertWord(res.data,Type);
            } else {
              ERRS('部门调用Err: '+res.message)
            }
          })
        }
      },
      //转换成通用的select
      insertWord(data,Type){
        this.selectUserAllot=data.map((v)=>{
          if(Type=='USER')
            this.$set(v,'allotName',v.username)
          else if(Type=='DEPARTMENT')
            this.$set(v,'allotName',v.departmentName)
          else if(Type=='POSITION')
            this.$set(v,'allotName',v.positionName)
          return v
        })
        console.log(111,this.selectUserAllot)
      },
      //t_fl_dy_node  审核id user_node
      FlowSubmitUserNode(){
        console.log(this.temp)
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            disableBtn('cfmbtm1');
            createOne(`flowable/dy/${this.temp.companyId }/${this.temp.groupId}/${this.temp.nodeId}/nodeOpUser`,this.temp).then(res => {
              if (res.code === 0) {
                SUCS('设置成功!')
              } else {
                ERRS(res.message)
              }
              this.dialogStatus = false
              this.divVisible = false

            })
          }
        })
      },


    }
  }
</script>
<style>
  .flowdiv{
    border-top: 1px dotted;
    margin-top: 3vh;
    padding-top: 3vh;
  }

  .el-dialog {
    min-height: 40%;
  }
</style>

